﻿/*******************************************************************************
Copyright (c) 2011-2013, PKStudio
Copyright (c) 2011,      Zynga Inc.
Copyright (c) 2008-2010, Ricardo Quesada

http://pkstudio.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

using System;
using System.Diagnostics;
using System.Drawing;

namespace PKEngine
{
	/// <summary>
	/// Interface of object has opacity and color property.
	/// </summary>
	public interface IPKRgba
	{
		int Opacity { get; set; }
		int ColorOpacity { get; set; }
		ushort Color { get; set; }
		int DisplayedOpacity { get; }
		int DisplayedColorOpacity { get; }
		ushort DisplayedColor { get; }
		bool CascadeOpacityEnabled { get; set; }
		bool CascadeColorEnabled { get; set; }
		void UpdateDisplayedOpacity(int parentOpacity);
		void UpdateDisplayedColor(ushort parentColor, int parentColorOpacity);
	}

	public interface IPKHue
	{
		int Hue { get; set; }
		bool CascadeHueEnabled { get; set; }
		void UpdateHue(int parentHue);
	}

	public interface IPKDisplayMode
	{
		PKSpriteDisplayMode DisplayMode { get; set; }
	}

	/// <summary>
	/// Display mode enum for sprite.
	/// </summary>
	public enum PKSpriteDisplayMode
	{
		Fast = 0, // Opacity and Alpha Channel will not take effect in this mode.
		Alpha,
		Add,
		Sub,
		Gray,
		Hue,
		Tint,
		Color,
		Transition,
		Channel,
		Default = Alpha
	}

	/// <summary>
	/// Sprite of engine. It is a 2d image can be drawn on the screen.
	/// Wikipedia: http://en.wikipedia.org/wiki/Sprite_(computer_graphics)
	/// </summary>
	public class PKSprite : PKNode, IPKRgba, IPKHue, IPKDisplayMode
	{
		#region Private Fields
		private PKSurface surface;
		private Rectangle rect;
		private int realOpacity;
		private int hue;
		private int realColorOpacity;
		private ushort realColor;
		private PKChannel transitionChannel;
		private int transitionValue;
		private PKChannel alphaChannel;
		#endregion

		#region Public Properties
		/// <summary>
		/// Surface of PKSprite.
		/// </summary>
		public PKSurface Surface
		{
			get
			{
				return this.surface;
			}
			protected set
			{
				if (value != this.surface)
				{
					if (this.surface == null || value == null || !this.surface.ContentSize.Equals(value.ContentSize))
					{
						this.transitionChannel = null;
						this.alphaChannel = null;
					}
					this.surface = value;
				}
			}
		}

		/// <summary>
		/// Rect of PKSprite.
		/// </summary>
		public Rectangle Rect
		{
			get
			{
				return this.rect;
			}
			protected set
			{
				if (!this.rect.Equals(value))
				{
					this.rect = value;
					this.ContentSize = new Size(value.Width, value.Height);
				}
			}
		}

		/// <summary>
		/// Opacity of PKSprite. Used for Alpha display mode.
		/// Range: 0 ~ 256
		/// </summary>
		public int Opacity
		{
			get
			{
				return this.realOpacity;
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				else if (value > 256)
				{
					value = 256;
				}
				this.DisplayedOpacity = realOpacity = value;

				if (this.CascadeOpacityEnabled)
				{
					int parentOpacity = 256;
					if (this.Parent != null && this.Parent is IPKRgba && ((IPKRgba)this.Parent).CascadeOpacityEnabled)
					{
						parentOpacity = ((IPKRgba)this.Parent).DisplayedOpacity;
					}
					this.UpdateDisplayedOpacity(parentOpacity);
				}
			}
		}

		/// <summary>
		/// ColorOpacity of PKSprite.
		/// Range: 0 ~ 256
		/// </summary>
		public int ColorOpacity
		{
			get
			{
				return this.realColorOpacity;
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				else if (value > 256)
				{
					value = 256;
				}
				this.DisplayedColorOpacity = this.realColorOpacity = value;

				if (this.CascadeColorEnabled)
				{
					ushort parentColor = PKAlpha.SharedAlpha.ColorWhite;
					int parentColorOpacity = 256;
					if (this.Parent != null && this.Parent is IPKRgba && ((IPKRgba)this.Parent).CascadeColorEnabled)
					{
						parentColor = ((IPKRgba)this.Parent).DisplayedColor;
						parentColorOpacity = ((IPKRgba)this.Parent).DisplayedColorOpacity;
					}
					this.UpdateDisplayedColor(parentColor, parentColorOpacity);
				}
			}
		}

		/// <summary>
		/// Color of PKSprite.
		/// </summary>
		public ushort Color
		{
			get
			{
				return this.realColor;
			}
			set
			{
				this.DisplayedColor = this.realColor = value;

				if (this.CascadeColorEnabled)
				{
					ushort parentColor = PKAlpha.SharedAlpha.ColorWhite;
					int parentColorOpacity = 256;
					if (this.Parent != null && this.Parent is IPKRgba && ((IPKRgba)this.Parent).CascadeColorEnabled)
					{
						parentColor = ((IPKRgba)this.Parent).DisplayedColor;
						parentColorOpacity = ((IPKRgba)this.Parent).DisplayedColorOpacity;
					}
					this.UpdateDisplayedColor(parentColor, parentColorOpacity);
				}
			}
		}

		/// <summary>
		/// Hue of PKSprite. Used for Hue display mode.
		/// Range: 0 ~ 359
		/// </summary>
		public int Hue
		{
			get
			{
				return this.hue;
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				else if (value > 359)
				{
					value = 359;
				}
				this.hue = value;

				if (this.CascadeHueEnabled)
				{
					int parentHue = value;
					if (this.Parent != null && this.Parent is IPKHue && ((IPKHue)this.Parent).CascadeHueEnabled)
					{
						parentHue = ((IPKHue)this.Parent).Hue;
					}
					this.UpdateHue(parentHue);
				}
			}
		}

		/// <summary>
		/// Displayed Opacity.
		/// </summary>
		public int DisplayedOpacity { get; private set; }

		/// <summary>
		/// Displayed ColorOpacity.
		/// </summary>
		public int DisplayedColorOpacity { get; private set; }

		/// <summary>
		/// Displayed Color.
		/// </summary>
		public ushort DisplayedColor { get; private set; }

		/// <summary>
		/// Whether or not opacity should be propagated to its children.
		/// </summary>
		public bool CascadeOpacityEnabled { get; set; }

		/// <summary>
		/// Whether or not hue should be propagated to its children.
		/// </summary>
		public bool CascadeHueEnabled { get; set; }

		/// <summary>
		/// Whether or not color should be propagated to its children.
		/// </summary>
		public bool CascadeColorEnabled { get; set; }

		/// <summary>
		/// TransitionChannel of PKSprite. Used for Transition display mode.
		/// </summary>
		public PKChannel TransitionChannel
		{
			get
			{
				return this.transitionChannel;
			}
			set
			{
				this.transitionChannel = null;
				if (value != null && this.Surface != null && value.ContentSize.Equals(this.Surface.ContentSize))
				{
					this.transitionChannel = value;
				}
			}
		}

		/// <summary>
		/// TransitionValue of PKSprite. Used for Transition display mode.
		/// Range: 0 ~ 256
		/// </summary>
		public int TransitionValue
		{
			get
			{
				return this.transitionValue;
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				else if (value > 256)
				{
					value = 256;
				}
				this.transitionValue = value;
			}
		}

		/// <summary>
		/// AlphaChannel of PKSprite. Used for Channel display mode.
		/// </summary>
		public PKChannel AlphaChannel
		{
			get
			{
				return this.alphaChannel;
			}
			set
			{
				this.alphaChannel = null;
				if (value != null && this.Surface != null && value.ContentSize.Equals(this.Surface.ContentSize))
				{
					this.alphaChannel = value;
				}
			}
		}

		/// <summary>
		/// DisplayMode of PKSprite.
		/// </summary>
		public PKSpriteDisplayMode DisplayMode { get; set; }
		#endregion

		#region Initialization
		protected PKSprite()
			: base()
		{
			this.DisplayedColor = this.Color = PKAlpha.SharedAlpha.ColorKey;
			this.DisplayedOpacity = this.Opacity = this.DisplayedColorOpacity = this.ColorOpacity = 256;
			this.CascadeOpacityEnabled = this.CascadeHueEnabled = this.CascadeColorEnabled = false;
			this.Hue = 0;
			this.DisplayMode = PKSpriteDisplayMode.Default;
		}

		public PKSprite(PKSurface surface)
			: this()
		{
			Debug.Assert(surface != null, "PKSprite: Argument cannot be null.");

			this.Surface = surface;
			this.Rect = new Rectangle(0, 0, surface.ContentSize.Width, surface.ContentSize.Height);
		}

		public PKSprite(PKSurface surface, Rectangle rect)
			: this()
		{
			Debug.Assert(surface != null, "PKSprite: Argument cannot be null.");

			this.Surface = surface;
			this.Rect = rect;
		}

		public PKSprite(string fileName)
			: this()
		{
			Debug.Assert(!string.IsNullOrEmpty(fileName), "PKSprite: Argument cannot be null.");

			PKSurface surface = PKSurfaceCache.SharedSurfaceCache.AddImage(fileName);
			if (surface != null)
			{
				this.Surface = surface;
				this.Rect = new Rectangle(0, 0, surface.ContentSize.Width, surface.ContentSize.Height);
			}
		}

		public PKSprite(string fileName, Rectangle rect)
			: this()
		{
			Debug.Assert(!string.IsNullOrEmpty(fileName), "PKSprite: Argument cannot be null.");

			PKSurface surface = PKSurfaceCache.SharedSurfaceCache.AddImage(fileName);
			if (surface != null)
			{
				this.Surface = surface;
				this.Rect = rect;
			}
		}
		#endregion

		#region IPKRgba & IPKHue Methods
		/// <summary>
		/// Recursive method that updates display opacity.
		/// </summary>
		public void UpdateDisplayedOpacity(int parentOpacity)
		{
			this.DisplayedOpacity = realOpacity * parentOpacity / 256;

			if (this.CascadeOpacityEnabled)
			{
				foreach (PKNode child in this.Children)
				{
					if (child is IPKRgba)
					{
						((IPKRgba)child).UpdateDisplayedOpacity(this.DisplayedOpacity);
					}
				}
			}
		}

		/// <summary>
		/// Recursive method that updates display color.
		/// </summary>
		public void UpdateDisplayedColor(ushort parentColor, int parentColorOpacity)
		{
			this.DisplayedColorOpacity = parentColorOpacity + realColorOpacity * (256 - parentColorOpacity) / 256;
			this.DisplayedColor = PKAlpha.SharedAlpha.PixelAlpha(this.realColor, parentColor, realColorOpacity);

			if (this.CascadeColorEnabled)
			{
				foreach (PKNode child in this.Children)
				{
					if (child is IPKRgba)
					{
						((IPKRgba)child).UpdateDisplayedColor(this.DisplayedColor, this.DisplayedColorOpacity);
					}
				}
			}
		}

		/// <summary>
		/// Recursive method that updates hue.
		/// </summary>
		public void UpdateHue(int parentHue)
		{
			this.Hue = parentHue;

			if (this.CascadeHueEnabled)
			{
				foreach (PKNode child in this.Children)
				{
					if (child is IPKHue)
					{
						((IPKHue)child).UpdateHue(this.Hue);
					}
				}
			}
		}
		#endregion

		#region Publib Methods
		/// <summary>
		/// Sets a new display frame to the PKSprite.
		/// </summary>
		/// <param name="frame">Specified frame</param>
		public void SetDisplayFrame(PKSpriteFrame frame)
		{
			this.Surface = frame.Surface;
			this.Rect = frame.Rect;
		}

		/// <summary>
		/// Returns whether or not a PKSpriteFrame is being displayed.
		/// </summary>
		/// <param name="frame">Specified frame</param>
		/// <returns>Is being displayed</returns>
		public bool IsFrameDisplayed(PKSpriteFrame frame)
		{
			if (this.Surface == frame.Surface && this.Rect.Equals(frame.Rect))
			{
				return true;
			}
			return false;
		}

		/// <summary>
		/// Returns the current displayed frame.
		/// </summary>
		/// <returns>Displayed frame</returns>
		public PKSpriteFrame GetDisplayFrame()
		{
			return new PKSpriteFrame(this.Surface, this.Rect);
		}

		/// <summary>
		/// Draws sprite.
		/// </summary>
		public override void Draw()
		{
			switch (DisplayMode)
			{
				case PKSpriteDisplayMode.Fast:
					PKMainSurface.SharedMainSurface.Blt(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect);
					break;
				case PKSpriteDisplayMode.Alpha:
					PKMainSurface.SharedMainSurface.BltAlpha(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect, this.DisplayedOpacity);
					break;
				case PKSpriteDisplayMode.Add:
					PKMainSurface.SharedMainSurface.BltAdd(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect);
					break;
				case PKSpriteDisplayMode.Sub:
					PKMainSurface.SharedMainSurface.BltSub(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect);
					break;
				case PKSpriteDisplayMode.Gray:
					PKMainSurface.SharedMainSurface.BltGray(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect);
					break;
				case PKSpriteDisplayMode.Hue:
					PKMainSurface.SharedMainSurface.BltHue(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect, this.Hue);
					break;
				case PKSpriteDisplayMode.Tint:
					PKMainSurface.SharedMainSurface.BltTint(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect, this.DisplayedColor, this.DisplayedColorOpacity);
					break;
				case PKSpriteDisplayMode.Color:
					PKMainSurface.SharedMainSurface.BltColor(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect, this.DisplayedColor);
					break;
				case PKSpriteDisplayMode.Transition:
					PKMainSurface.SharedMainSurface.BltWithTransitionChannel(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect, this.TransitionChannel, this.TransitionValue);
					break;
				case PKSpriteDisplayMode.Channel:
					PKMainSurface.SharedMainSurface.BltWithAlphaChannel(this.BoundingBox.X, this.BoundingBox.Y, this.Surface, this.Rect, this.AlphaChannel);
					break;
			}
		}
		#endregion
	}
}
